﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;
using MShop.Core.Util;

namespace MShop.Core.DataAccess
{
    public class PayDao : CommonDao<Pay>, IPayDao
    {
        private readonly IPayPluginDao _PayPluginDao;
        public PayDao(IObjectContextProvider provider,IPayPluginDao PayPluginDao)
            : base(provider)
        {
            _PayPluginDao = PayPluginDao;
        }

        public override Pay GetById(object oid)
        {
            var m= base.GetById(oid);
            var payModel = _PayPluginDao.GetPayModel(int.Parse(m.PayType));

            m.SetPayModel((IPayModel)SerializeUtil.Deserialize(payModel.GetType(),m.PayValues));
            return m;
        }

        public virtual Pay GetById(Type type)
        {
            PayPlugin plug= _PayPluginDao.GetPayModel(type);
            string s = plug.Id.ToString();
            var payModel = _PayPluginDao.GetPayModel(plug.Id);
            var m = Find(x => x.PayType == s).SingleOrDefault();
           
            if (m != null)
            {
                m.SetPayModel((IPayModel)SerializeUtil.Deserialize(payModel.GetType(), m.PayValues));
            }
            return m;
        }
       
    }
}